package com.example.mq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;

import javax.swing.*;
import java.nio.charset.StandardCharsets;

public class ProgressConsumer implements ChannelAwareMessageListener {

    private JProgressBar progressBar;

    public ProgressConsumer(JProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        // 处理 RabbitMQ 队列中的消息，更新进度条
        String progressMessage = new String(message.getBody(), StandardCharsets.UTF_8);
        int progress = Integer.parseInt(progressMessage);
        System.out.println(progress);

        SwingUtilities.invokeLater(() -> {
            progressBar.setValue(progress);
        });

        // 手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}

